<?php
class util
{
    private $template;
    
    public function __construct()
    {
        //require_once '../init.define.php';
        require_once PATH_INCLUDES. '/FCKeditor/fckeditor.php'; 
        require_once 'template.class.php';
        require_once 'editor.class.php'; 
        $this->template = new template();  
    }
    
    public function autorun()
    {
    }
    
    /**
    *   @Name: generate_page
    *   @Desc: generate a whole page, a section of page depend on caller
    *   @Access: public
    *   @Param: string
    *   @Param: string
    *   @Return: void
    */
    public function generate_page($template_path = '', $div_id='content')
    {   
       return $this->template->get_template_file($template_path);
       
        
        //Ajax but not work >"<
        //$this->template->update_element($div_id,$body);  
        //$this->template->go();
    }
    
    public function load_page($template_path = '', $div_id='content'){
        return $this->template->load_template_file($template_path);    
    }
    //Sort data with key as $key
    public function sort_array($data,$key)
    {
        $length = sizeof($data);
        for($j=0; $j<$length;$j++) {
        for($i=0;$i<$length-$j-1;$i++){
            if(strcmp($data[$i][$key],$data[$i+1][$key]) >0) 
            {
                $temp = $data[$i];
                $data[$i] = $data[$i+1];
                $data[$i+1] = $temp;
            }
        }}
        return $data;
    }
    //Send data to template
    public function send_var($var_name, $var_value)
    {
        $this->template->send_var($var_name, $var_value);
    }
     public function send_pointer($pointer_name, $pointer_value)
    {
        $this->template->send_pointer($pointer_name, $pointer_value);
    }
    //---------------------------------------------------------
    public function create_editor($name='des',$value='', $width='600', $height='350'){
        require_once(PATH_INCLUDES."/FCKeditor/fckeditor.php");

        //Fck editor configuration

        $oFCKeditor = new FCKeditor($name);
        $oFCKeditor->BasePath = '../includes/FCKeditor/';
        $oFCKeditor->Value = $value;
        $oFCKeditor->Width = '95%' ;
        $oFCKeditor->Height = '500' ;
        $oFCKeditor->ToolbarSet = 'Default';
        //$editor_create=$oFCKeditor->CreateHtml(); 
        return $oFCKeditor;
    }
    
    // Upload file to server
    //Param: $file: file posted
    //Param: $dir: Folder name to store file
    public function uploader($file, $dir){
        $result_data = array();
        $dir_store = '../data/'.$dir ;
        if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/pjpeg") || ($_FILES[$file]["type"] == "image/jpg")) && ($_FILES[$file]["size"] < 20971520))
          {
          if ($_FILES[$file]["error"] > 0)
            {
            $result_data['error'] = FILE_ERROR;
            }
          else
            {
            if (file_exists($dir_store . $_FILES[$file]["name"]))
              {
               $result_data['error'] = FILE_EXISTS; 
              }
            else
              {
              move_uploaded_file($_FILES[$file]["tmp_name"], $dir_store . $_FILES[$file]["name"]);
              }
            }
          }
        else
          {
          $result_data['error'] = FILE_INVALID_TYPE; 
          }
        //Return data to database
        $result_data['name'] = $_FILES[$file]["name"];
        $result_data['size'] = number_format(($_FILES[$file]["size"] / 1048576),2) . 'MB'; 
        isset($result_data['error'])?intval($result_data['error']):0;
        return $result_data;
  }
  // Upload file to server
    //Param: $file: file posted
    //Param: $dir: Folder name to store file
    public function uploaderMusic($file, $dir){
        $result_data = array();
        $dir_store = '../data/'.$dir ;
        if ($_FILES[$file]["size"] < 20971520)
          {
          if ($_FILES[$file]["error"] > 0)
            {
            $result_data['error'] = FILE_ERROR;
            }
          else
            {
            if (file_exists($dir_store . $_FILES[$file]["name"]))
              {
               $result_data['error'] = FILE_EXISTS; 
              }
            else
              {
              move_uploaded_file($_FILES[$file]["tmp_name"], $dir_store . $_FILES[$file]["name"]);
              }
            }
          }
        else
          {
          $result_data['error'] = FILE_INVALID_TYPE; 
          }
        //Return data to database
        $result_data['name'] = $_FILES[$file]["name"];
        $result_data['size'] = number_format(($_FILES[$file]["size"] / 1048576),2) . 'MB'; 
        isset($result_data['error'])?intval($result_data['error']):0;
        return $result_data;
  }
  // Upload image to server
    //Param: $file: file posted
    //Param: $dir: Folder name to store file
    public function uploaderImage($file, $dir){
        $result_data = array();
        $dir_store = '../data/'.$dir ;
        if ($_FILES[$file]["size"] < 20971520)
          {
          if ($_FILES[$file]["error"] > 0)
            {
            $result_data['error'] = FILE_ERROR;
            }
          else
            {
            if (file_exists($dir_store . $_FILES[$file]["name"]))
              {
               $result_data['error'] = FILE_EXISTS; 
              }
            else
              {
              move_uploaded_file($_FILES[$file]["tmp_name"], $dir_store . $_FILES[$file]["name"]);
              }
            }
          }
        else
          {
          $result_data['error'] = FILE_INVALID_TYPE; 
          }
        //Return data to database
        $result_data['name'] = $_FILES[$file]["name"];
        $result_data['size'] = number_format(($_FILES[$file]["size"] / 1048576),2) . 'MB'; 
        isset($result_data['error'])?intval($result_data['error']):0;
        return $result_data;
  }
}
?>
